package com.wayhome.srpingbootmybatis.entity;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class Person {
    int id;
    String name;

    // Constructor, getters, setters
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }


    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        // 假设这里有重复的id
        people.add(new Person(1, "Alice"));
        people.add(new Person(2, "Bob"));
        people.add(new Person(1, "Alice")); // 重复元素
        people.add(new Person(3, "Charlie"));

        List<Person> distinctPeople = people.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(Person::getId, person -> person, (p1, p2) -> p1),
                        map -> new ArrayList<>(map.values())
                ));
        System.out.println(distinctPeople);
    }
}
